//
//  SelectDiscountCouponsView.m
//  PigMarket
//
//  Created by mac on 2019/5/17.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "SelectDiscountCouponsView.h"
#import "DiscountCouponCell.h"

@interface SelectDiscountCouponsView ()<UITableViewDelegate,UITableViewDataSource,UIGestureRecognizerDelegate>
/**
 bgview
 */
@property (nonatomic , strong) UIView *bgView;
/**
 dataBg
 */
@property (nonatomic , strong) UIView *dataBgView;
/**
 headView
 */
@property (nonatomic , strong) UIView *headView;
/**
 请选择豚券
 */
@property (nonatomic , strong) UILabel *headTitle;
/**
 tableview
 */
@property (nonatomic , strong) UITableView *myTableView;

@end

static SelectDiscountCouponsView *instance = nil;
@implementation SelectDiscountCouponsView


#pragma mark - init method
+ (instancetype)sharedInstance {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[SelectDiscountCouponsView alloc] init];
    });
    return instance;
}
- (instancetype)init
{
    self = [super init];
    if (self) {
        [self initView];
        [self initLayout];
        [self initAction];
        [self initData];
    }
    return self;
}

#pragma mark - init method

- (void)initView {
    [self addSubview:self.bgView];
    [self.bgView addSubview:self.dataBgView];
    [self.dataBgView addSubview:self.headView];
    [self.dataBgView addSubview:self.headTitle];
    [self.dataBgView addSubview:self.confirmBtn];
    [self.dataBgView addSubview:self.myTableView];
}

- (void) initLayout {
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    [self.dataBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
        make.height.mas_equalTo(431);
    }];
    [self.headView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_equalTo(0);
        make.height.mas_equalTo(50);
    }];
    [self.headTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.centerY.mas_equalTo(self.headView.mas_centerY);
        make.height.mas_equalTo(22);
    }];
    [self.confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.centerY.mas_equalTo(self.headView.mas_centerY);
        make.height.mas_equalTo(22);
    }];
    [self.myTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(50, 0, 0, 0));
    }];
}


- (void) initAction {
    [self.confirmBtn addTarget:self action:@selector(confirmClick) forControlEvents:UIControlEventTouchUpInside];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hide)];
    tap.delegate = self;
    [self.bgView addGestureRecognizer:tap];
}

- (void)initData {

}

#pragma mark - delegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [[UIView alloc] init];
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] init];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    DiscountCouponCell *cell = [DiscountCouponCell cellWithTableView:tableView];
    DiscountCoupons *model = self.dataArr[indexPath.row];
    [cell configCellWithModel:model discountCouponsType:DiscountCouponsTypeSelect];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    DiscountCoupons *model = self.dataArr[indexPath.row];
    model.isSelect = !model.isSelect;
    [self.myTableView reloadData];
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    if ([touch.view isDescendantOfView:self.dataBgView]) {
        return NO;
    }
    return YES;
}
#pragma mark - target action
- (void)confirmClick {
    NSMutableArray *tempArr = [[NSMutableArray alloc] init];
    
    //确定时 清空上次数据
    [tempArr removeAllObjects];
    
    int selectMoneyCount = 0;
    int selectMoney = 0;
    for (DiscountCoupons *model in self.dataArr) {
        if (model.isSelect) {
            [tempArr addObject:model.meCouponId];
            selectMoney = [model.couponValue intValue];
            selectMoneyCount += selectMoney;
        }
    }
    if (self.selectedDiscountCouponsBlock) {
        self.selectedDiscountCouponsBlock(selectMoneyCount, tempArr);
    }
    [self hide];
}
#pragma mark - private method
- (void)showDiscountCouponsView{
    self.alpha = 0;
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 0.5;
    }];
}
- (void)hide {
    
    [self removeFromSuperview];
}
#pragma mark - getter and setter
- (UIView *)bgView {
    if (!_bgView) {
        _bgView = [[UIView alloc] init];
        _bgView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
    }
    return _bgView;
}
- (UIView *)dataBgView {
    if (!_dataBgView) {
        _dataBgView = [[UIView alloc] init];
        _dataBgView.backgroundColor = [UIColor whiteColor];
    }
    return _dataBgView;
}
- (UIView *)headView {
    if (!_headView) {
        _headView = [[UIView alloc] init];
    }
    return _headView;
}
- (UILabel *)headTitle {
    if (!_headTitle) {
        _headTitle = [[UILabel alloc] init];
        _headTitle.textColor = [UIColor textBlackColor];
        _headTitle.font = [UIFont largerFont];
        _headTitle.text = @"请选择豚券";
    }
    return _headTitle;
}
- (UIButton *)confirmBtn {
    if (!_confirmBtn) {
        _confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_confirmBtn setTitle:@"确定" forState:UIControlStateNormal];
        [_confirmBtn setTitleColor:[UIColor primaryColor] forState:UIControlStateNormal];
        _confirmBtn.titleLabel.font = [UIFont largerFont];
    }
    return _confirmBtn;
}

- (UITableView *)myTableView {
    if (!_myTableView) {
        _myTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _myTableView.rowHeight = 115;
        _myTableView.delegate = self;
        _myTableView.dataSource = self;
    }
    return _myTableView;
}
- (NSMutableArray *)dataArr {
    if (!_dataArr) {
        _dataArr = [[NSMutableArray alloc] init];
    }
    return _dataArr;
}

@end
